package service;

import common.Message;
import common.MessageType;
import entity.User;

import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Array;

public class UserService {

    private Socket client;
    private ObjectOutputStream output;
    private ObjectInputStream input;

    public UserService() {
        try {
            client = new Socket("localhost", 9999);
            output = new ObjectOutputStream(client.getOutputStream());
            input = new ObjectInputStream(client.getInputStream());
            System.out.println("连接服务器成功！");
        } catch (IOException e) {
            System.out.println("连接服务器失败！");
            e.printStackTrace();
            close(client, output, input);
        }
    }

    public Socket getClient() {
        return client;
    }

    public void myStop() {
        close(client, input, output);
        client = null;
        input = null;
        output = null;
    }

    /**
     * 用户登录
     *
     * @param qq
     * @param pwd
     * @return
     */
    public Message login(int qq, String pwd) {
        try {
            // 将用户信息发送服务器登录
            User user = new User(qq, pwd);
            Message message = new Message(MessageType.USER_LOGIN);
            message.setContent(user);
            output.writeObject(message);

            // 接收服务器返回到结果
            Message msg = (Message) input.readObject();
            return msg;
        } catch (IOException | ClassNotFoundException e) {
            close(client, output, input);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 用户注册
     *
     * @param avatar
     * @param nickName
     * @param sign
     * @param pwd
     * @return
     */
    public User register(String avatar, String nickName, String sign, String pwd) {
        try {
            // 将用户信息发送服务器
            User user = new User(nickName, pwd, sign, avatar);
            Message message = new Message(MessageType.REGISTER);
            message.setContent(user);
            output.writeObject(message);

            // 接收服务器返回到结果
            Message msg = (Message) input.readObject();
            User content = (User) msg.getContent();
            return content;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 修改密码
     *
     * @param qq
     * @param pwd
     * @return
     */
    public Boolean changPassword(int qq, String pwd) {
        try {
            // 将用户信息发送服务器
            User user = new User();
            user.setPwd(pwd);
            user.setQq(qq);
            Message message = new Message(MessageType.CHANGE_PWD);
            message.setContent(user);
            output.writeObject(message);

            // 接收服务器返回到结果
            Message msg = (Message) input.readObject();
            Boolean content = (Boolean) msg.getContent();
            return content;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean addFriend(int qq, int fqq) {
        try {
            // 将用户信息发送服务器
            int[] qqs = {qq, fqq};
            Message message = new Message(MessageType.ADD_FRIEND);
            message.setContent(qqs);
            output.writeObject(message);

            // 接收服务器返回到结果
            Message msg = (Message) input.readObject();
            Boolean content = (Boolean) msg.getContent();
            return content;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 用于关闭多个io流
     *
     * @param ios
     */
    private void close(Closeable... ios) {//可变长参数
        for (Closeable io : ios) {
            try {
                if (null != io) {
                    io.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
